//
//  FromPositionViewController.m
//  Qya
//
//  Created by maxwell pang on 12-9-8.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "FromPositionViewController.h"
#import "MyQYaViewController.h"

@interface FromPositionViewController ()

@end

@implementation FromPositionViewController

@synthesize mAddStringLabel;
@synthesize mNowPositionImageView;
@synthesize mMyAddPositionImageView;

@synthesize mMyAddButton1;
@synthesize mMyAddButton2;
@synthesize mMyAddButton3;
@synthesize mMyAddButton4;
@synthesize mMyAddButton5;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        mPositionArray = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(selectConfirmAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    [mNowPositionImageView setImage:[UIImage imageNamed:@"woxianzaideweizhi"]];
    [mMyAddPositionImageView setImage:[UIImage imageNamed:@"wotianjiadeweizhi"]];
    mMyAddButton1.tag = 11331;
    mMyAddButton2.tag = 11332;
    mMyAddButton3.tag = 11333;
    mMyAddButton4.tag = 11334;
    mMyAddButton5.tag = 11335;
}

- (void)viewDidUnload
{
    [self setMNowPositionImageView:nil];
    [self setMMyAddPositionImageView:nil];
    [self setMAddStringLabel:nil];
    [self setMMyAddButton1:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)selectConfirmAction:(id)sender  
{  
    [self.navigationController setToolbarHidden:YES animated:NO];
//    [self.navigationController popToRootViewControllerAnimated:YES];

    MyQYaViewController *secondView = [[MyQYaViewController alloc] init];  
    [self.navigationController pushViewController:secondView animated:YES];  
    secondView.title = @"我的Q芽";  
}

- (IBAction)onAddPosition:(id)sender {
    
    [mAddStringLabel resignFirstResponder];
    
    [mPositionArray insertObject:mAddStringLabel.text atIndex:0];
    while ([mPositionArray count] > 5)
    {
        [mPositionArray removeLastObject];
    }
    while ([mPositionArray count] < 5) {
        [mPositionArray insertObject:@"" atIndex:([mPositionArray count]-1)];
    }
    [self refreshMyAddPosion];
}

- (IBAction)onDeletePosition:(id)sender
{
    while ([mPositionArray count] > 5)
    {
        [mPositionArray removeLastObject];
    }
    while ([mPositionArray count] < 5) {
        [mPositionArray insertObject:@"" atIndex:([mPositionArray count]-1)];
    }
    for (NSInteger i=11331, number=0; i<=11335; ++i, ++number)
    {
        UIButton *button = (UIButton *)[self.view viewWithTag:i];
        if (button == sender)
        {
            [mPositionArray removeObjectAtIndex:number];
        }
    }
    while ([mPositionArray count] > 5)
    {
        [mPositionArray removeLastObject];
    }
    while ([mPositionArray count] < 5) {
        [mPositionArray insertObject:@"" atIndex:([mPositionArray count]-1)];
    }
    [self refreshMyAddPosion];
}

- (void)refreshMyAddPosion
{
    for (NSInteger i=11331, number=0; i<=11335; ++i, ++number)
    {
        UIButton *button = (UIButton *)[self.view viewWithTag:i];
        [button setTitle:[mPositionArray objectAtIndex:number] forState:UIControlStateNormal];
    }
}

@end
